<?php
namespace app\controllers\api\post;

use fate\http\Request;
use app\models\Posts;
use app\libs\LibUser;
use app\libs\Html;
use app\libs\Util;
use app\libs\Sys;
use app\libs\Uploader;

class EditController extends \fate\web\PureController {

    public function run() {
        global $_G;

        // 是否登录
        $uid = LibUser::getLoginUid();
        if('' === $uid) {
            $this->ajaxReturn(null, 200001);
            exit;
        }

        // 关闭投稿
        if(!Sys::canPost($uid)) {
            $this->ajaxReturn(null, 500002);
            exit;
        }

        $req = Request::getInstance();
        $id = $req->getParameter('id');
        if(!is_numeric($id)) {
            $this->ajaxReturn(null, 300002);
            exit;
        }

        // 有无权限修改 兼顾检查文章是否存在
        $postModel = new Posts();
        $belong = $postModel->isBelongTo($id, $uid);
        if(!$belong) {
            $this->ajaxReturn(null, 200002);
            exit;
        }

        $articleConfig = Sys::getArticleLengthConfig();
        $thumb = $req->getParameter('thumb');
        $title = $req->getParameter('title');
        $content = $req->getParameter('content');
        $tags = $req->getParameter('tags');

        $readCateId = $req->getParameter('cate_id');
        $copyright = $req->getParameter('copyright');
        $brief = empty($req->getParameter('brief')) ? '' : $req->getParameter('brief');

        // check
        if(!is_numeric($readCateId)) {
            $this->ajaxReturn(null, 300002);
            exit;
        }
        if(!is_numeric($copyright)) {
            $this->ajaxReturn(null, 300002);
            exit;
        }
        if(!$thumb) {
            $this->ajaxReturn(null, 300002);
            exit;
        }
        if(!$title) {
            $this->ajaxReturn(null, 300002);
            exit;
        }

        $plain = Util::plainText($content);
        if( Util::strlenOfCN($plain) < $articleConfig['min'] || Util::strlenOfCN($plain) > $articleConfig['max'] ) {
            $this->ajaxReturn(null, 300002);
            exit;
        }

        $brief = Util::plainText($brief);
        if(!$brief) {
            $brief = Util::substrOfCN($plain, 0, 150);
        }

        // 保存头图 只处理 base64
        if(0 === strpos($thumb, 'data:')) {
            $uploader = new Uploader();
            $uploader->setConfigs([
                'basePath' => './upload'
            ]);
            $uploader->saveBase64Image($thumb);
            $thumb = $uploader->getFirstUploadedFile();

            if('' === $thumb) {
                $this->ajaxReturn(null, 300003);
                exit;
            }

            // and prefix
            $thumb = '/upload' . $thumb;
        }

        $filter = new Html();
        // var_dump($filter->pure($content));exit;

        $postModel->update($id, [
            'thumb' => $thumb,
            'title' => $title,
            'content' => $filter->pure($content),
            'tags' => $tags,
            'cate_id' => $readCateId,
            'copyright' => $copyright,
            'brief' => $brief,
            'status' => 0
        ]);

        $this->ajaxReturn(null);
    }

}
